package com.js.common.exception;

import cn.dev33.satoken.exception.NotLoginException;
import cn.hutool.core.util.IdUtil;
import cn.hutool.http.HttpStatus;
import com.js.domian.model.AjaxResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 全局异常处理器
 * 
 * @author lw
 */
@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 自定义的异常处理
     * @param e
     * @return
     */
    @ExceptionHandler(AjaxException.class)
    public @ResponseBody AjaxResult error(AjaxException e){
        log.error("===================>" + e.getMessage());
        return AjaxResult.fail(e.getMessage());
    }

    /**
     * 登录异常处理
     * @param e
     * @return
     */
    @ExceptionHandler(NotLoginException.class)
    public @ResponseBody AjaxResult notLogin(NotLoginException e){
        log.error("===================>未登录或登录已过期-{}", e.getMessage());
        return AjaxResult.fail("未登录或登录已过期").put("status", HttpStatus.HTTP_UNAUTHORIZED);
    }

    /**
     * 所有异常捕抓
     */
    @ExceptionHandler(Exception.class)
    public @ResponseBody AjaxResult allException(Exception e) {
    	log.error("====================>系统异常["+IdUtil.fastSimpleUUID()+"]", e);
        return AjaxResult.fail("系统异常["+IdUtil.fastSimpleUUID()+"]");
    }
}
